package com.qubuyer.business.mine.viewmodel

import androidx.lifecycle.MutableLiveData
import com.qubuyer.core.anno.AutoInject
import com.qubuyer.core.http.RetrofitCall
import com.qubuyer.repository.entitys.IncomeEntity
import com.qubuyer.repository.entitys.RebateOrderEntity
import com.qubuyer.repository.entitys.SaleAmountEntity
import com.qubuyer.repository.entitys.WalletInfoEntity

class WalletViewModel : UserViewModel() {
    val wallet = MutableLiveData<WalletInfoEntity>()
    val rebateOrder = MutableLiveData<RebateOrderEntity>()
    var incomeType: String? = null
    var saleAmountType: Int = 1
    var rebateOrderType: Int = 1
    var rebateOrderId: Int = 0
    private var page = 1

    @AutoInject
    private val model = WalletModel()

    fun getWalletInfo() {
        RetrofitCall<WalletInfoEntity?>(lifecycleOwner, this) {
            api { model.getWalletInfo() }
            success {
                wallet.value = it
            }
        }
    }


    fun getVerificationCode(block: () -> Unit) {
        userEntity.value?.let {
            RetrofitCall<String?>(lifecycleOwner, this) {
                api { authModel.getCode(it.mobile ?: "") }
                success {
                    block.invoke()
                }
            }
        }
    }

    fun withdraw(withdrawMoney: String, type: String, code: String, block: () -> Unit) {
        userEntity.value?.let {
            RetrofitCall<String?>(lifecycleOwner, this) {
                api { model.withdraw(withdrawMoney, type, it.mobile ?: "", code) }
                success {
                    block.invoke()
                }
            }
        }
    }

    fun refreshIncome(block: (success: Boolean, dataList: List<IncomeEntity>?, page: Int, fromCache: Boolean) -> Unit) {
        page = 1
        RetrofitCall<List<IncomeEntity>?>(lifecycleOwner, this) {
            api { model.loadIncome(page, incomeType) }
            success {
                block.invoke(true, it, page, false)
            }

            fail { list, _, _ ->
                block.invoke(false, list, page, false)
            }
        }
    }

    fun loadMoreIncome(block: (success: Boolean, dataList: List<IncomeEntity>?, page: Int, fromCache: Boolean) -> Unit) {
        page++
        RetrofitCall<List<IncomeEntity>?>(lifecycleOwner, this) {
            api { model.loadIncome(page, incomeType) }
            success {
                block.invoke(true, it, page, false)
            }

            fail { list, _, _ ->
                block.invoke(false, list, page, false)
            }
        }
    }

    fun refreshRebateOrder(block: (success: Boolean, dataList: List<RebateOrderEntity>?, page: Int, fromCache: Boolean) -> Unit) {
        page = 1
        RetrofitCall<List<RebateOrderEntity>?>(lifecycleOwner, this) {
            api { model.loadRebateOrderList(page, rebateOrderType) }
            success {
                block.invoke(true, it, page, false)
            }

            fail { list, _, _ ->
                block.invoke(false, list, page, false)
            }
        }
    }

    fun loadMoreRebateOrder(block: (success: Boolean, dataList: List<RebateOrderEntity>?, page: Int, fromCache: Boolean) -> Unit) {
        page++
        RetrofitCall<List<RebateOrderEntity>?>(lifecycleOwner, this) {
            api { model.loadRebateOrderList(page, rebateOrderType) }
            success {
                block.invoke(true, it, page, false)
            }

            fail { list, _, _ ->
                block.invoke(false, list, page, false)
            }
        }
    }

    fun loadRebateOrderDetail() {
        RetrofitCall<RebateOrderEntity?>(lifecycleOwner, this) {
            api { model.loadRebateOrderDetail(rebateOrderId) }
            success {
                rebateOrder.postValue(it)
            }
        }
    }

    fun restoreBalance(block: () -> Unit) {
        RetrofitCall<String?>(lifecycleOwner, this) {
            api { model.restoreBalance(rebateOrderId) }
            success {
                block.invoke()
            }
        }
    }

    fun refreshSaleAmount(block: (success: Boolean, dataList: SaleAmountEntity?, page: Int, fromCache: Boolean) -> Unit) {
        page = 1
        RetrofitCall<SaleAmountEntity?>(lifecycleOwner, this) {
            api { model.loadSaleAmount(page, saleAmountType) }
            success {
                block.invoke(true, it, page, false)
            }

            fail { list, _, _ ->
                block.invoke(false, list, page, false)
            }
        }
    }

    fun loadMoreSaleAmount(block: (success: Boolean, dataList: SaleAmountEntity?, page: Int, fromCache: Boolean) -> Unit) {
        page++
        RetrofitCall<SaleAmountEntity?>(lifecycleOwner, this) {
            api { model.loadSaleAmount(page, saleAmountType) }
            success {
                block.invoke(true, it, page, false)
            }

            fail { list, _, _ ->
                block.invoke(false, list, page, false)
            }
        }
    }
}